local json = require("cjson")


local cmds = require "cmds"

local green_ip = {["120.27.232.226"]=1,
                  ["218.107.55.252"]=1,
                   ["119.130.106.213"]=1,
                     ["123.57.16.126"]=1,  
                       ["58.56.226.2"]=1, 
                         ["118.31.78.219"]=1, 
                         ["127.0.0.1"]=1, 
                                        
                  
            }

--获取客户端ip
function get_client_ip()
    local headers=ngx.req.get_headers()
    local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
    return ip
end


local client_ip = get_client_ip()
if not green_ip[client_ip] then
    local msg = {code=10, 
              msg="your ip cant access this api "}
      ngx.say(json.encode(msg))
  return
end


local arg = ngx.req.get_uri_args()

local func_name = arg.func

if not func_name then
  local msg = {code=2, 
              msg="no func"}
  ngx.say(json.encode(msg))
  return
end

local func = cmds[func_name]
if not func then
    local msg = {code=3, 
                  msg="func error"}
    ngx.say(json.encode(msg))
    return 
end

local ret, info = func(cmds, arg)

local msg = {code=ret, 
             msg=info}

ngx.say(json.encode(msg))

